package com.mrs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrs.common.ResponseResult;
import com.mrs.common.enums.AppHttpCodeEnum;
import com.mrs.common.vo.PageVo;
import com.mrs.common.vo.TagVo;
import com.mrs.dto.TagListDto;
import com.mrs.dto.TagsDto;
import com.mrs.entity.Tag;
import com.mrs.exception.SystemException;
import com.mrs.mapper.TagMapper;
import com.mrs.service.TagService;
import com.mrs.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-09-10 10:07:17
 */
@Service("TagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    /**
     * @description:  标签管理页面分页查询, 可能有查询条件(标签名，备注)
     * @param pageNum
     * @param pageSize
     * @param tagListDto
     * @return: com.mrs.common.ResponseResult<com.mrs.common.vo.PageVo>
     * @author: MR.孙
     * @date: 2022/9/11 16:22
    */
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {

        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        //如果前端传递的参数中包含标签名则添加查询条件
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()), Tag::getName,
                tagListDto.getName());
        //如果前端传递的参数中包含备注则添加查询条件
        queryWrapper.like(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark,
                tagListDto.getRemark());


        Page<Tag>  page = new Page<>(pageNum,pageSize);
        page(page, queryWrapper);

        return ResponseResult.okResult(new PageVo(page.getRecords(), page.getTotal()));
    }

    /**
     * @description:  新增博客标签
     * @param tagListDto
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/9/11 17:44
    */
    @Override
    public ResponseResult addTags(TagListDto tagListDto) {
        //新增的标签名称或者备注不能为空
        if(!StringUtils.hasText(tagListDto.getName())){
            throw new SystemException(AppHttpCodeEnum.NAME_NOT_NULL);
        }
        if(!StringUtils.hasText(tagListDto.getRemark())){
            throw new SystemException(AppHttpCodeEnum.REMARK_NOT_NULL);
        }
        Tag  tag = new Tag();
        tag.setName(tagListDto.getName());
        tag.setRemark(tagListDto.getRemark());
        save(tag);
        return ResponseResult.okResult();
    }


    /**
     * @description: 删除博客标签
     * @param id
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/9/11 18:08
    */
    @Override
    public ResponseResult removeTags(Long id) {
        if(Objects.isNull(id)){
            throw new SystemException(AppHttpCodeEnum.ID_NOT_NULL);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    
    /**
     * @description:  根据id获取博客标签信息
     * @param id
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/9/11 18:23
    */
    @Override
    public ResponseResult getTagsById(Long id) {
        if(Objects.isNull(id)){
            throw new SystemException(AppHttpCodeEnum.ID_NOT_NULL);
        }
        Tag tag = getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", tag.getName());
        map.put("remark", tag.getRemark());
        return ResponseResult.okResult(map);
    }


    /**
     * @description:  修改标签信息
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/9/11 18:42
    */
    @Override
    public ResponseResult updateTagsInfo(TagsDto tagsDto) {
        if(!StringUtils.hasText(tagsDto.getId())){
            throw new SystemException(AppHttpCodeEnum.ID_NOT_NULL);
        }

        if(!StringUtils.hasText(tagsDto.getName())){
            throw new SystemException(AppHttpCodeEnum.NAME_NOT_NULL);
        }

        if(!StringUtils.hasText(tagsDto.getRemark())){
            throw new SystemException(AppHttpCodeEnum.REMARK_NOT_NULL);
        }

        //添加查询条件
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getId,tagsDto.getId());

        Tag tag = new Tag();
        tag.setName(tagsDto.getName());
        tag.setRemark(tagsDto.getRemark());

        update(tag, queryWrapper);
        return ResponseResult.okResult();
    }


    /**
     * @description:  查询所有标签
     * @return: com.mrs.common.ResponseResult
     * @author: MR.孙
     * @date: 2022/9/11 23:05
    */
    @Override
    public ResponseResult listAllTag() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        //查询部分字段
        queryWrapper.select(Tag::getId,Tag::getName);

        //查询所有标签
        List<Tag> tagList = list(queryWrapper);

        //封装数据并返回
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tagList, TagVo.class);

        return ResponseResult.okResult(tagVos);
    }
}
